package aiyh.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionManager;

import java.io.File;
import java.io.Reader;

/**
 * @author EBU7-dev1-ayh
 * create 2021/12/14 0014 15:57
 */


public class MybatisUtil {
    private static SqlSessionManager sqlSessionManager = null;

    private synchronized  static void init(String config){
        try {
            Reader resourceAsReader = Resources.getResourceAsReader("WEB-INF" + File.separator +config);
            sqlSessionManager = SqlSessionManager.newInstance(resourceAsReader);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static SqlSessionManager getSessionManager(String config){
        if(sqlSessionManager == null){
            synchronized (MybatisUtil.class) {
                if (sqlSessionManager == null) {
                    init(config);
                }
            }
        }
        return sqlSessionManager;
    }
}
